<% layout('/layouts/default.html', {title: '列检调度确认小票', libs: ['dataGrid']}){ %>
<div class="main-content">
	<div class="box box-main">
		<div class="box-header">
			<div class="box-title">
				<i class="fa icon-notebook"></i> ${text('列检调度确认小票')}
			</div>
			<div class="box-tools pull-right">
				<a href="#" class="btn btn-default" id="btnSearch" title="${text('查询')}"><i class="fa fa-filter"></i> ${text('查询')}</a>
				<a href="#" class="btn btn-default" id="btnSetting" title="${text('设置')}"><i class="fa fa-navicon"></i></a>
			</div>
		</div>
		<div class="box-body">
			<#form:form id="searchForm" model="${tblXdJxchsd}" action="${ctx}/tbl/replyform/tblXdJxchsd/listData3" method="post" class="form-inline"
					data-page-no="${parameter.pageNo}" data-page-size="${parameter.pageSize}" data-order-by="${parameter.orderBy}">


				<div class="form-group">
					<label class="control-label">${text('日期')}：</label>
					<div class="control-inline">
						<#form:input path="stationTime" readonly="true"  class="form-control laydate"
						dataFormat="date" data-type="date" data-format="yyyy-MM-dd" />
					</div>
				</div>
				<div class="form-group">
					<label class="control-label">${text('班别')}：</label>
					<div class="control-inline width-120">
						<#form:select path="station" dictType="shift_type" blankOption="true" class="form-control"/>
					</div>
				</div>

				<div class="form-group">
					<label class="control-label">${text('列检调度是否确认')}：</label>
					<div class="control-inline width-120">
						<#form:select path="status" dictType="sys_yes_no" blankOption="true" blankOption="true" class="form-control"/>
					</div>
				</div>
				<div class="form-group">
					<div class="control-inline">
						<#form:checkbox path="remark" dictType="like_select" class="form-control"/>
					</div>
				</div>
				<div class="form-group">
					<!-- <label class="control-label">${text('车号模糊查询')}：</label> -->
					<div class="control-inline">
						<#form:input path="wagonno" maxlength="6" class="form-control width-120"/>
					</div>
				</div>
				
				<div class="form-group">
					<button type="submit" class="btn btn-primary btn-sm">${text('查询')}</button>
					<button type="reset" class="btn btn-default btn-sm">${text('重置')}</button>
					<a class="btn btn-primary btn-sm" onclick="confirm()">  确认  </a>
					<a class="btn btn-primary btn-sm" onclick="removeSc()">  撤销  </a>
				</div>
			</#form:form>
			<table id="dataGrid"></table>
			<div id="dataGridPage"></div>
		</div>
	</div>
</div>
<% } %>
<script>
/* function cjbnChange(){
	var deptCode = $("#stationCode").find("option:selected").text();
	var params={"deptCode":$.trim(deptCode)};
	console.log(deptCode);
	$.post("${ctx}/tbl/replyform/tblXdJxchsd/findCsbhList",params,function(data){
		var csbh = $("#csbh")[0];  //清空原来下拉框的值
		csbh.options.length = 1;//加空白选项

		//进行循环判断并追加数据到二级下拉框中
		for (var i of data) {
			csbh.options.add(new Option(i.csmc, i.csbh));  //往下拉框中追加值
		}
		
	},"json");
} */
$(function(){
	// 定义一个函数来更新班次和日期
	function updateShiftAndTime() {
		var date_now = new Date();
		var hours = date_now.getHours();
		var year = date_now.getFullYear();  // 获取年
		var month = date_now.getMonth() + 1;  // 获取月
		var day = date_now.getDate();  // 获取日
		var $stationTime = $("#stationTime");
		console.log(hours);
		// 设置班次和日期
		if (hours >= 19) {
			// 夜班 (19:00到0:00)，日期保持不变
			date_now.setDate(date_now.getDate() + 1);// 日期加一天
			day = date_now.getDate();  // 更新日
			month = date_now.getMonth() + 1;  // 更新月
			year = date_now.getFullYear();  // 更新年
		} else if (hours < 8) {
			// 夜班 (0:00到8:00)，日期减去一天
			date_now.setDate(date_now.getDate()); // 日期
			year = date_now.getFullYear();  // 更新年
			month = date_now.getMonth() + 1;  // 更新月
			day = date_now.getDate();  // 更新日
		}

		// 设置日期，补零处理
		$stationTime.val(year + "-" + (month < 10 ? '0' + month : month) + "-" + (day < 10 ? '0' + day : day));
	}

	// 页面加载时更新一次
	updateShiftAndTime();

	// 每隔一分钟更新一次班次和日期
	// setInterval(updateShiftAndTime, 60000);
});

// 初始化DataGrid对象
$('#dataGrid').dataGrid({
	searchForm: $("#searchForm"),
	multiselect:true,
	rownumWidth:40,
	shrinkToFit:false,
	columnModel: [
		{header:'${text("是否确认")}', name:'	status', index:'a.status', width:150, align:"center", formatter: function(val, obj, row, act){
			return js.getDictLabel(${@DictUtils.getDictListJson('hsd_status')}, row.status, '${text("未知")}', true);
		}},
		{header:'${text("运用号")}', name:'wagonno', index:'a.wagonno', width:150, align:"center", frozen:true, formatter: function(val, obj, row, act){
			return '<a href="${ctx}/tbl/replyform/tblXdJxchsd/form?recId='+row.recId+'" class="btnList" data-title="${text("编辑检修车回送单")}">'+(val||row.id)+'</a>';
		}},
		{header:'${text("入修地点")}', name:'repairLineName', index:'a.repair_line_name', width:150, align:"center"},
		{header:'${text("扣车原因")}', name:'faultName', index:'a.fault_name', width:150, align:"center"},
		{header:'${text("入修类别")}', name:'repairMethod', index:'a.repair_method', width:150, align:"center"},
		{header:'${text("入修方式")}', name:'repairMethodDetail', index:'a.repair_method_detail', width:150, align:"center"},
		{header:'${text("是否禁峰")}', name:'isNopeak', index:'a.is_nopeak', width:150, align:"center", formatter: function(val, obj, row, act){
				return js.getDictLabel(${@DictUtils.getDictListJson('sys_yes_no')}, val, '${text("未知")}', true);
			}},
		{header:'${text("限速")}', name:'speedLimit', index:'a.speed_limit', width:150, align:"center"},
		{header:'${text("通知时间")}', name:'stationTime', index:'a.station_time', width:180, align:"center"},
		{header:'${text("办理时间")}', name:'workStartTime', index:'a.work_start_time', width:180, align:"center"},
		{header:'${text("检车人")}', name:'jcrName', index:'a.jcr_name', width:150, align:"center"},
		{header:'${text("列检员")}', name:'ljUsername', index:'a.lj_username', width:150, align:"center"},
		{header:'${text("列检调度")}', name:'ljddUsername', index:'a.ljdd_username', width:150, align:"center"},
		{header:'${text("车站调度")}', name:'czddUsername', index:'a.czdd_username', width:150, align:"center"},
		{header:'${text("更新人")}', name:'userName', index:'a.user_name', width:150, align:"center"},
		{header:'${text("更新时间")}', name:'updTime', index:'a.upd_time', width:180, align:"center"},
		{header:'${text("备注")}', name:'remark', index:'a.remark', width:150, align:"center"}
	],
	// 加载成功后执行事件
	ajaxSuccess: function(data){
		const array = JSON.parse(sessionStorage.getItem("列检调度确认小票"))
		if(array && array.length > 0){
			for (const item of array) {
				$('#dataGrid').dataGrid('setSelectRow', item, false);
			}
		}
	},
	onSelectRow: function(id, isSelect, event){
		const array = $('#dataGrid').dataGrid('getSelectRows')
		sessionStorage.setItem("列检调度确认小票", JSON.stringify(array))
	},
}).dataGrid('setLabel', 'rn', '序号');

$('button[type="reset"]').on('click', function() {
	//移除暂存
	sessionStorage.removeItem("列检调度确认小票")
	$('#dataGrid').dataGrid('resetSelection');
})

//确认
function confirm(){
	var ids = $("#dataGrid").jqGrid("getGridParam", "selarrrow");
	console.log(ids);
	if(ids.length>0){
		var id_str = "";
		for (var a=0; a<ids.length; a++) {
			/* var rowData = $('#dataGrid').dataGrid('getRowData', ids[a]);
			console.log(rowData.remark);
			if(rowData.status != "1"){
				js.showMessage('第'+(a+1)+'条记录已确认，无法重复确认操作！', '', 'warning', 4000);
				return;
			} */
			id_str += ids[a]+",";
		}
		
		var params={"ids":id_str.slice(0,-1),"type_LC":"1"};
		console.log(id_str.slice(0,-1));
		$.post("${ctx}/tbl/replyform/tblXdJxchsd/confirmData",params,function(data){
			if(data.result == "false"){
				js.showMessage(data.message, '', 'erro', 4000);
			}else{
				js.showMessage('确认成功！', '', 'success', 4000);
				// 刷新页面
				$("#searchForm").submit();
			}
		},"json");
	}else{
		 js.showMessage('请选择记录。', '', 'warning', 4000);
		 return;
	}
}

//撤销
function removeSc(){
	var ids = $("#dataGrid").jqGrid("getGridParam", "selarrrow");
	console.log(ids);
	if(ids.length>0){
		var id_str = "";
		for (var a=0; a<ids.length; a++) {
			id_str += ids[a]+",";
		}
		
		var params={"ids":id_str.slice(0,-1),"type_LC":"3"};
		$.post("${ctx}/tbl/replyform/tblXdJxchsd/removeData",params,function(data){
			if(data.result == "false"){
				js.showMessage(data.message, '', 'erro', 4000);
			}else{
				js.showMessage('撤销成功！', '', 'success', 4000);
				// 刷新页面
				$("#searchForm").submit();
			}
		},"json");
	}else{
		 js.showMessage('请选择记录。', '', 'warning', 4000);
		 return;
	}
}
</script>
<style>
	.laydate-btns-clear{
		display: none !important;
	}
</style>